package com.tianci.appstore.ui.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.SkinManager;
import com.tianci.appstore.ui.util.UICommon;
import com.tianci.appstore.utils.SkyConstants;


public class TipToast {
    private static TipToast instance;
//    private static LinkedList<ToastInfo> toShowList;

    private Context mContext;
    private Toast toast;
    private ImageView iconImage;
    private TextView toastText;

    private Handler controlHandler;
    private boolean isToastShowing = false;
    public static boolean firstReceiver = true;
    public static final int HIDE_TOAST = 1;

    public static int defaultDuration = 2000;

    private int offsetX = SkyConstants.Div(118);
    private int offsetY = SkyConstants.Div(41);

    public static TipToast getInstance(Context context) {
        if (instance == null)
            instance = new TipToast(context.getApplicationContext());
        return instance;
    }

    private TipToast(Context c) {
        mContext = c;
//        toShowList = new LinkedList<ToastInfo>();

        initToast();
        controlHandler = new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(Message msg) {
                switch (msg.what) {
                    case HIDE_TOAST:
//                        LogUtils.d("tip_toast", "receive hide toast msg, crt list size = "+toShowList.size());
//                        if(toShowList.size() > 0)
//                        {
//                            isToastShowing = false;
//                            toast.cancel();
//                            ToastInfo info = toShowList.poll();
//                            showDialog(info);
//                        }
//                        else{
//                            isToastShowing = false;
                        toast.cancel();
//                        }
                        break;
                }
                super.handleMessage(msg);
            }
        };
    }

    private void initToast() {
        toast = new Toast(mContext);
        toast.setGravity(Gravity.RIGHT | Gravity.BOTTOM,
                SkyConstants.Div(118), SkyConstants.Div(41));

        LinearLayout layout = new LinearLayout(mContext);
        layout.setGravity(Gravity.CENTER_VERTICAL);
        SkinManager.setBackground(layout, R.drawable.toast_bg);
        toast.setView(layout);

        iconImage = new ImageView(mContext);
        LayoutParams iconParams = new LayoutParams(SkyConstants.Div(53),
                SkyConstants.Div(59));
        iconParams.setMargins(SkyConstants.Div(33), SkyConstants.Div(23),
                SkyConstants.Div(24), SkyConstants.Div(23));
        layout.addView(iconImage, iconParams);

        toastText = UICommon.newTextView(mContext, SkyConstants.toastTextSize,
                R.color.toastTextColor);
        toastText.setSingleLine(true);
        toastText.setPadding(SkyConstants.Div(20), 0, SkyConstants.Div(33), 0);
        layout.addView(toastText, new LinearLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    }

    private void showDialog(ToastInfo info) {
        isToastShowing = true;
        iconImage.setImageDrawable(info.iconDrawable);
        toastText.setText(info.text);
        toast.setGravity(info.gravity, info.x, info.y);

        LogUtils.i("toast", "------duration = " + info.duration);
        if (controlHandler.hasMessages(HIDE_TOAST))
            controlHandler.removeMessages(HIDE_TOAST);
        toast.setDuration(info.duration);
        toast.show();
        LogUtils.d("toast", "    after, toast duration = " + toast.getDuration());

        controlHandler.sendEmptyMessageDelayed(HIDE_TOAST, info.duration);
    }

    public void showToast(Drawable iconDrawable, String text, int duration, int gravity, int x, int y) {
        ToastInfo info = new ToastInfo(iconDrawable, text, gravity, duration, x, y);
//        toShowList.add(info);
//        notifyToast();
        showDialog(info);
    }

    // 此为参数最全的一个toast，可完成下面所有showToast方法的功能
    public void showToast(Drawable iconDrawable, String text, int duration, int gravity) {
        int x = 0;
        int y = 0;
        switch (gravity) {
            case Gravity.TOP | Gravity.LEFT:
            case Gravity.TOP | Gravity.RIGHT:
            case Gravity.BOTTOM | Gravity.RIGHT:
            case Gravity.BOTTOM | Gravity.LEFT:
                x = offsetX;
                y = offsetY;
                break;
            case Gravity.TOP:
            case Gravity.BOTTOM:
                x = 0;
                y = offsetY;
                break;
            case Gravity.LEFT:
            case Gravity.RIGHT:
                x = offsetX;
                y = 0;
                break;
            case Gravity.CENTER:
                x = 0;
                y = 0;
                break;
            default:
                break;
        }
        showToast(iconDrawable, text, duration, gravity, x, y);
    }

    //    public void showToast(Bitmap iconBitmap, String text)
//    {
//        showToast(new BitmapDrawable(iconBitmap), text);
//    }
//    
//    private void notifyToast()
//    {
//        LogUtils.d("tip_toast", "isShowing = "+isToastShowing);
//        ToastInfo info = toShowList.poll();
//        showDialog(info);
//    }
//
//    // 将此toast加入待显示队列中
//    public void showToast(Drawable iconDrawable, String text)
//    {
//        toShowList.add(new ToastInfo(iconDrawable, text));
//        notifyToast();
//    }
//    
//    // 将此toast加入待显示队列中
//    public void showToastCenter(Drawable iconDrawable, String text)
//    {
//        toShowList.add(new ToastInfo(iconDrawable, text, Gravity.CENTER));
//        notifyToast();
//    }
//    
//    // 将此toast加入待显示队列中
//    public void showToastCenter(Drawable iconDrawable, String text, int duration)
//    {
//        toShowList.add(new ToastInfo(iconDrawable, text, Gravity.CENTER, duration));
//        notifyToast();
//    }
//
//    // 立刻显示此toast
//    public void showToastNow(Drawable iconDrawable, String text)
//    {
//        showDialog(new ToastInfo(iconDrawable, text));
//    }
//
//    // 将此toast加入待显示队列中
//    public void showToast(Drawable iconDrawable, String text, int gravity)
//    {
//        toShowList.add(new ToastInfo(iconDrawable, text, gravity));
//        notifyToast();
//    }
//
//    // 立刻显示此toast
//    public void showToastNow(Drawable iconDrawable, String text, int gravity)
//    {
//        showDialog(new ToastInfo(iconDrawable, text, gravity));
//    }
//    
    public void showToast(Bitmap bm, String text) {
        showToast(new BitmapDrawable(bm), text);
    }

    public void showToast(Drawable iconDrawable, String text) {
        showToast(iconDrawable, text, defaultDuration, Gravity.RIGHT | Gravity.BOTTOM);
    }

    public void showTipToast(String text) {
        showToast(SkinManager.getResDrawable(R.drawable.toast_icon_tip), text);
    }

//    // 网络未连接提示
//    public void showNetNotConnectedToast()
//    {
//        LogUtils.d("tip_toast", "showing net not connected toast");
//        showToast(SkinManager.getResDrawable(R.drawable.toast_icon_net_disconnected), 
//                mContext.getResources().getString(R.string.net_not_connected_tip));
//    }

    public static final Runnable showNetIsNotConnectedToastRunnable = new Runnable() {
        @Override
        public void run() {
            TipToast.getInstance(CoocaaApplication.getContext()).showNetChangeToast(false);
        }
    };

    public void showNetChangeToast(boolean connected) {
        if (connected) {
            showToast(SkinManager.getResDrawable(R.drawable.toast_icon_net_connected),
                    mContext.getResources().getString(R.string.net_connect_tip));
        } else {
            showToast(SkinManager.getResDrawable(R.drawable.toast_icon_net_disconnected),
                    mContext.getResources().getString(R.string.net_not_connected_tip));
        }
    }

    // 网络加载慢提示
    public void showNetSlowToast() {
        showToast(SkinManager.getResDrawable(R.drawable.toast_icon_net_connected),
                mContext.getResources().getString(R.string.net_slow_tip));
    }

    public void clearToast() {
        toast.cancel();
//        toShowList.clear();
    }

    class ToastInfo {
        public Drawable iconDrawable;
        public String text;
        int gravity = Gravity.RIGHT | Gravity.BOTTOM;
        int duration;
        int x, y;

        public ToastInfo(Drawable iconDrawable, String text, int gravity, int duration, int x, int y) {
            this.iconDrawable = iconDrawable;
            this.text = text;
            this.gravity = gravity;
            this.duration = duration;
            this.x = x;
            this.y = y;
        }

        public ToastInfo(Drawable iconDrawable, String text, int gravity, int duration) {
            this.iconDrawable = iconDrawable;
            this.text = text;
            this.gravity = gravity;
            this.duration = duration;
            if (gravity == (Gravity.RIGHT | Gravity.BOTTOM)) {
                this.x = SkyConstants.Div(118);
                this.y = SkyConstants.Div(41);
            } else {
                this.x = 0;
                this.y = 0;
            }
        }

        public ToastInfo(Drawable iconDrawable, String text, int gravity) {
            this(iconDrawable, text, gravity, TipToast.this.defaultDuration);
        }

        public ToastInfo(Drawable iconDrawable, String text) {
            this(iconDrawable, text, Gravity.RIGHT | Gravity.BOTTOM, TipToast.this.defaultDuration);
        }
    }
}
